package com.prs.crm.action.person;

import java.io.File;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.person.Contract;
import com.prs.crm.domain.person.Dossier;
import com.prs.crm.domain.person.Limit;
import com.prs.crm.domain.person.PersonnelContract;
import com.prs.crm.service.person.ContractService;
import com.prs.crm.service.person.DossierService;
import com.prs.crm.service.person.LimitService;
import com.prs.crm.service.person.PersonnelContractService;

public class PersonnelContractAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private PersonnelContract personnelContract;
	private PersonnelContractService personnelContractService;
	private List<Dossier> dossier;
	private DossierService dossierService;
	private List<Contract> contract;
	private ContractService contractService;
	private List<Limit> limit;
	private LimitService limitService;
	public List<Dossier> getDossier() {
		return dossier;
	}
	public void setDossier(List<Dossier> dossier) {
		this.dossier = dossier;
	}
	public DossierService getDossierService() {
		return dossierService;
	}
	public void setDossierService(DossierService dossierService) {
		this.dossierService = dossierService;
	}
	public List<Contract> getContract() {
		return contract;
	}
	public void setContract(List<Contract> contract) {
		this.contract = contract;
	}
	public ContractService getContractService() {
		return contractService;
	}
	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}
	public List<Limit> getLimit() {
		return limit;
	}
	public void setLimit(List<Limit> limit) {
		this.limit = limit;
	}
	public LimitService getLimitService() {
		return limitService;
	}
	public void setLimitService(LimitService limitService) {
		this.limitService = limitService;
	}

	private File upload;
	private String fileName; 
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public PersonnelContract getPersonnelContract() {
		return personnelContract;
	}
	public void setPersonnelContract(PersonnelContract personnelContract) {
		this.personnelContract = personnelContract;
	}
	public PersonnelContractService getPersonnelContractService() {
		return personnelContractService;
	}
	public void setPersonnelContractService(
			PersonnelContractService personnelContractService) {
		this.personnelContractService = personnelContractService;
	}
	
	@Action("listPersonnelContract")
	public String listPersonnelContract(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(PersonnelContract.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPersonnelContractService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editPersonnelContract")
	public String editPersonnelContract(){
		if (this.ids!=null) {
			this.personnelContract=this.personnelContractService.get(ids[0]);
		}
		this.dossier=this.dossierService.getAllDossier();
		this.contract=this.contractService.getAllContract();
		this.limit=this.limitService.getAllLimit();
		return SUCCESS;
	}
	@Action("viewPersonnelContract")
	public String viewPersonnelContract(){
		if (this.ids!=null) {
			this.personnelContract=this.personnelContractService.get(ids[0]);
		}
		return SUCCESS;
	}
	@Action(value = "savePersonnelContract", results = {
			@Result(name = "success", type = "redirect", location = "listPersonnelContract"),
			@Result(name = "error", type = "chain", location = "editPersonnelContract")
			})
	public String savePersonnelContract(){
		if (upload!=null) {
			this.personnelContract.setAttach(FileUtil.getInstance().createAttach(upload, fileName));
		}
		this.getPersonnelContractService().save(personnelContract);
		return SUCCESS;
	}
	
	@Action(value = "removePersonnelContract", results = {
			@Result(name = "success", type = "redirect", location = "listPersonnelContract"),
			@Result(name = "error", type = "chain", location = "listPersonnelContract")
			})
	public String removePersonnelContract(){
		if(ids==null){
			this.addActionError("请选择一行进行操作！");
			return ERROR;
		}
		this.getPersonnelContractService().batchRemove(ids);
		return SUCCESS;
	}
}
